<template>
  <div class="box">
    <el-row>
      <el-col :span="24">
        <div class="header">
          <el-form ref="form" :model="form" :rules="rules" label-width="100px" class="flex-bettom">
            <div class="flex">
              <el-form-item label="网点名称" prop="name" style="margin-right: 40px">
                <el-select v-model="form.name" placeholder="请选择网点名称" style="width: 286px" @change="lineChange">
                  <el-option v-for="item in newOptions" :key="item.id" :label="item.networkName" :value="item.networkName" />
                </el-select>
              </el-form-item>
              <el-form-item label="日期范围" prop="value1">
                <el-date-picker v-model="form.value1" type="daterange" format="yyyy-MM-dd" value-format="yyyy-MM-dd" :clearable="false" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions" />
              </el-form-item>
            </div>
            <el-form-item>
              <el-button type="primary" @click="search(currentIndex)">查询</el-button>
              <el-button @click="reset">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24" class="pie">
        <div class="flex-bettom">
          <p class="fenxi">出港流向分析</p>
          <div class="flex-bettom">
            <div v-for="(item, index) in kgList.slice(0, 9)" :key="index" :class="
                weightSection == item.weightSection
                  ? 'list-kg list-kg-hover'
                  : 'list-kg list-kgs'
              " @click="kgClick(item.weightSection)">
              <span>{{ item.name }}</span>
            </div>
            <el-popover placement="bottom" title="" trigger="click">
              <div v-for="(item1, index1) in kgList.slice(-5)" :key="index1" :class="
                  weightSection == item1.weightSection
                    ? 'list-kgs-hover list-kg'
                    : 'list-kg'
                " @click="kgClick(item1.weightSection)">
                <span>{{ item1.name }}</span>
              </div>
              <el-button slot="reference" class="kg-btn">更多</el-button>
            </el-popover>
          </div>
        </div>
        <div id="mychart" ref="mychart" style="padding-top: 20px" class="echart" />
      </el-col>
    </el-row>
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="汇总" name="1" />
      <el-tab-pane label="明细" name="2" />
    </el-tabs>
    <el-row type="flex" justify="space-between">
      <el-col :span="15">
        <!-- <div class="titles">当前页共<span class="nums">{{totalTable>10?10:totalTable}}</span>项</div> -->
      </el-col>
      <el-col :span="4">
        <div class="flex-end">
          <el-button @click="onSubmit"><i class="el-icon-upload2" />导出</el-button>
          <!-- <el-button @click="onSubmit">数据说明</el-button> -->
        </div>
      </el-col>
    </el-row>
    <elltable ref="multipleTable" :loading="loading" :data="tableData" :th="th" :n="4" />
    <div v-show="totalTable > 0" class="block">
      <div>
        共 <span class="nums">{{ totalTable == "" ? 0 : totalTable }}</span> 项
      </div>
      <pagination :page-size="form.pageSize" :current-page="form.page" :total="totalTable" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    </div>
  </div>
</template>

<script>
import * as echarts from 'echarts'
import { getNetworkInfo } from '@/api/branchFinance/wages/fixed'
import {
  searchDetail,
  collectDetail,
  collectAnalyze
} from '@/api/branchFinance/operation/flow'

export default {
  data() {
    return {
      pickerOptions: {
        disabledDate(time) {
          // 获取今天的日期
          const today = new Date()
          // 如果选择的日期在今天之后，则禁用
          return time.getTime() > today.getTime()
        }
      },
      activeName: '1',
      form: {
        name: '',
        page: 1,
        pageSize: 10
      },
      // 下拉框
      newOptions: [],
      searchCurrent: '',
      tableData: [],
      totalTable: null,
      rules: {
        name: [
          {
            required: true,
            message: '请选择网点名称',
            trigger: 'blur'
          }
        ],
        value1: [
          {
            required: true,
            message: '请选择日期范围',
            trigger: 'blur'
          }
        ]
      },
      currentIndex: '0',
      kgList: [
        {
          weightSection: 1,
          name: '全部'
        },
        {
          weightSection: 2,
          name: '0KG'
        },
        {
          weightSection: 3,
          name: '0.0-0.3KG'
        },
        {
          weightSection: 4,
          name: '0.3-0.5KG'
        },
        {
          weightSection: 5,
          name: '0.5-1.0KG'
        },
        {
          weightSection: 6,
          name: '1.0-1.5KG'
        },
        {
          weightSection: 7,
          name: '1.5-2.0KG'
        },
        {
          weightSection: 8,
          name: '2.0-2.5KG'
        },
        {
          weightSection: 9,
          name: '2.5-3.0KG'
        },
        {
          weightSection: 10,
          name: '3.0-5.0KG'
        },
        {
          weightSection: 11,
          name: '5.0-10.0KG'
        },
        {
          weightSection: 12,
          name: '10.0-30.0KG'
        },
        {
          weightSection: 13,
          name: '30.0-50.0KG'
        },
        {
          weightSection: 14,
          name: '50KG以上'
        }
      ],
      weightSection: 0,
      province: [],
      income: [],
      count: [],
      th: [],
      loading: false
    }
  },
  mounted() {
    this.initEcharts()
    this.getList()
  },
  methods: {
    // tab切换
    handleClick(tab) {
      console.log(tab)
      this.currentIndex = tab.index
      this.handleCurrentChange()
    },
    // 网点名称下拉框
    getList() {
      getNetworkInfo()
        .then((res) => {
          this.newOptions = res.data
        })
    },
    // 网点名称选中数据
    lineChange(e) {
      this.searchCurrent = e
    },
    // 图表
    kgClick(val) {
      this.weightSection = val
      const params = {
        startTime: this.form.value1 == null ? '' : this.form.value1[0],
        endTime: this.form.value1 == null ? '' : this.form.value1[1],
        networkNameId: this.searchCurrent,
        weightSection: this.weightSection
      }
      collectAnalyze(params)
        .then((res) => {
          this.count = res.data.count
          this.income = res.data.income
          this.province = res.data.province
          this.initEcharts()
        })
    },
    // 查询表格数据
    search(currentIndex) {
      if (this.form.name === '') {
        this.$message.warning('请选择网点名称!')
        return false
      } else if (this.form.value1 === [] || this.form.value1 == null) {
        this.$message.warning('请选择日期范围!')
        return false
      } else {
        this.kgClick(1)
        const params = {
          startTime: this.form.value1 == null ? '' : this.form.value1[0],
          endTime: this.form.value1 == null ? '' : this.form.value1[1],
          networkNameId: this.searchCurrent,
          pageDate: {
            pageNum: this.form.page,
            pageSize: this.form.pageSize
          }
        }
        this.loading = true
        if (this.currentIndex === '0') {
          // 汇总
          collectDetail(params)
            .then((res) => {
              this.loading = false

              this.tableData = res.data.list.data
              this.totalTable = res.data.list.total
              this.th = res.data.fields
            })
        } else {
          // 明细
          searchDetail(params)
            .then((res) => {
              this.loading = false

              this.tableData = res.data.list.data
              this.totalTable = res.data.list.total
              this.th = res.data.fields
            })
        }
        this.loading = false
      }
    },
    // 页容量改变时会触发
    handleSizeChange(size) {
      this.form.pageSize = size
      this.search()
    },
    // 当前页改变时会触发
    handleCurrentChange(currentPage) {
      this.form.page = currentPage
      this.search(this.currentIndex)
    },
    // 重置
    reset() {
      this.form.name = ''
      this.form.value1 = []
      this.searchCurrent = ''
      this.weightSection = 0
      this.province = undefined
      this.income = undefined
      this.count = undefined
      this.initEcharts()
      this.activeName = '1'
      this.tableData = []
      this.totalTable = 0
      this.th = []
      // this.$router.go(0);
    },
    // 导出
    onSubmit() { },
    // 柱状图
    initEcharts() {
      const option = {
        legend: {
          align: 'left',
          right: '1%',
          // top: '10%',
          type: 'plain',
          data: ['收入', '件量']
        },
        grid: {
          left: '1%',
          right: '1%',
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            crossStyle: {
              color: '#999'
            }
          }
        },

        xAxis: [
          {
            type: 'category',
            data: this.province,
            // axisPointer: {
            //   // type: 'shadow'
            // },
            axisLabel: {
              color: '#666666',
              fontSize: 14
            },
            axisLine: {
              show: true,
              lineStyle: {
                // color: '#DEDFE2'
              }
            },
            axisTick: {
              show: false
            }
            // splitLine: {
            //   show: false,
            //   lineStyle: {
            //     color: '#195384',
            //     type: "dotted",
            //   }
            // },
          }
        ],
        yAxis: [
          {
            type: 'value',
            name: '',
            // min: 0,
            // max: 250,
            interval: 50,
            axisLabel: {
              formatter: '{value}'
            },
            splitLine: {
              show: true,
              lineStyle: {
                color: ['#cdd2de'],
                type: 'dashed'
              }
            },
            axisLine: {
              show: false
            },
            axisTick: {
              show: false
            }
          },
          {
            type: 'value',
            name: '',
            // min: 0,
            // max: 25,
            interval: 5,
            axisLabel: {
              formatter: '{value}'
            },
            splitLine: {
              show: true,
              lineStyle: {
                color: ['#cdd2de'],
                type: 'dashed'
              }
            },
            axisLine: {
              show: false
            },
            axisTick: {
              show: false
            }
          }
        ],
        series: [
          {
            name: '收入',
            type: 'bar',
            barWidth: '5%',
            itemStyle: {
              color: '#2087D0',
              barBorderRadius: 20
            },
            // tooltip: {
            //   valueFormatter: function(value) {
            //     return value + ' ml'
            //   }
            // },
            data: this.income
          },

          {
            name: '件量',
            type: 'line',
            yAxisIndex: 1,
            itemStyle: {
              color: '#F4B259'
            },
            // tooltip: {
            //   valueFormatter: function(value) {
            //     return value + ' °C'
            //   }
            // },
            data: this.count
          }
        ]
      }
      const myChart = echarts.init(this.$refs.mychart) // 图标初始化
      myChart.setOption(option) // 渲染页面
      // 随着屏幕大小调节图表
      window.addEventListener('resize', () => {
        myChart.resize()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.box {
  padding: 20px;
}

.flex {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  flex-wrap: wrap;
}

.flex-bettom {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.header {
  // padding-bottom: 20px;
  border-bottom: 1px solid #d8d8d8;
}

.el-row {
  margin-bottom: 20px;

  &:last-child {
    margin-bottom: 0;
  }
}

.el-col {
  border-radius: 4px;
}

.bg-purple-dark {
  background: #99a9bf;
}

.flex-end {
  display: flex;
  justify-content: flex-end;
  align-items: center;
}

.bg-purple {
  background: #d3dce6;
}

.bg-purple-light {
  background: #e5e9f2;
}

.grid-content {
  border-radius: 4px;
  min-height: 36px;
}

.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}

.el-button {
  border-radius: 21px;
  padding: 10px 20px;
}

.el-button--primary {
  background-color: #2087d0;
}
.el-button--primary:hover {
  background-color: #2087d0cc !important;
}

.el-button--default {
  border-color: #b4b4b4;
  color: #666666;
}

::v-deep .el-table th {
  background-color: #ebecee;
  color: #666666;
}

::v-deep .el-table tr {
  color: #333333;
}

::v-deep .el-pager li {
  min-width: 28px;
}

::v-deep .el-pager li.active {
  border-radius: 21px;
  // border: 1px solid #B4B4B4;
  background-color: #2087d0;
  color: #ffffff;
}

::v-deep .el-pagination .el-select .el-input .el-input__inner {
  border-radius: 21px;
}

::v-deep .el-pagination__editor.el-input .el-input__inner {
  border-radius: 21px;
}

.nums {
  color: #2087d0;
}

.block {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 30px;
}

::v-deep .el-tabs__item {
  font-size: 16px;
}

.titles {
  font-size: 18px;
  margin-top: 18px;
  margin-bottom: 15px;
}

::v-deep .el-form-item__label {
  font-size: 18px;
}

::v-deep .el-tabs__item.is-active {
  color: #2087d0;
}

::v-deep .el-tabs__active-bar {
  color: #2087d0;
}

::v-deep .el-table th {
  background-color: #ebecee;
  color: #666666;
  height: 64px;
  font-size: 18px;
  font-weight: 400;
}

::v-deep .el-table tr {
  color: #333333;
  font-size: 18px;
  font-weight: 400;
}

::v-deep .el-form-item__label {
  font-size: 18px;
  color: #666666;
  font-weight: 400;
}

::v-deep .el-input__inner {
  border: 1px solid #b4b4b4;
  font-size: 16px;
  color: #333333;
}

::v-deep .el-range-editor .el-range-input {
  font-size: 16px;
  color: #333333;
}

.fenxi {
  font-weight: 400;
  font-size: 24px;
  color: #333333;
}

.list-kg {
  height: 36px;
  line-height: 35px;
  // border: 1px solid #B4B4B4;
  border-radius: 4px;
  margin-right: 16px;
}

.list-kg span {
  padding: 0 17px;
  font-weight: 400;
  font-size: 18px;
  cursor: pointer;
}

.list-kgs {
  border: 1px solid #b4b4b4;
}
.list-kgs:hover {
  border: 1px solid #2087d0c2;
  span {
    color: #2087d0c2;
  }
}
.list-kg:hover {
  span {
    color: #2087d0c2;
  }
}

.list-kgs span {
  color: #666666;
}

.list-kg-hover {
  border: 1px solid #2087d0;
}

.list-kg-hover span {
  color: #2087d0;
}

.list-kgs-hover {
  color: #2087d0;
}

// .list-kgs{
//   margin-right: 0;
//   margin-bottom: 10px;
//   border: none!important;
// }
// .list-kgs span:hover{
//   color: #2087D0!important;
//   // background-color: #2087D0;
// }

// .list-kgs span {
//   border: none;
// }
.kg-btn {
  font-size: 18px;
  font-weight: 400;
  height: 36px;
  // line-height: 36px;
  // padding: 11px 17px;
  border-radius: 4px;
}

.kg-btn:hover {
  color: #2087d0 !important;
  background-color: #ffffff;
  border: 1px solid #2087d0;
}

#mychart {
  width: 100%;
  height: 320px;
}

::v-deep .el-tabs__header {
  margin-bottom: 20px;
}
</style>
